# encoding: utf-8
#@author: sydr
#@file: localfixture.py
#@time: 2022/9/19 14:45
import pytest
import arrow
from pymysql import Connection
from pymysql.cursors import DictCursor, Cursor

from redisKeys.redis_keys import *


@pytest.fixture(scope="class")
def changePositionInit(redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_chapter set position=7 where id=530;"
    curDic.execute(sql)
    sql="update tms_chapter set position=6 where id=534;"
    curDic.execute(sql)
    sql="update tms_qtopic set subject_type=9 where subject_type=4 and (id<1589 or id >1698);"
    curDic.execute(sql)
    sql="update tms_qtopic set subject_type=10 where subject_type=6 and (id<1376 or id >1409);"
    curDic.execute(sql)
    sql="update tms_exam_point set frequency=2 where id=1662;"
    curDic.execute(sql)
    sql="delete from tms_qtopic_exam_point where (qtopic_id>=1589 and qtopic_id <1617) or (qtopic_id>1625 and qtopic_id<=1698)"
    curDic.execute(sql)
    sql="delete from tms_qtopic_exam_point where qtopic_id>=1376 and qtopic_id <=1409 and qtopic_id not in (1376,1379,1382,1385,1388,1392,1395,1399,1403,1408);"
    curDic.execute(sql)
    con.commit()
    toDay=arrow.now().format().split(" ")[0]
    key1=QTOPIC_DONE_TOPICS%(3,4,userId)
    key2=QTOPIC_CORRECT_POOL%(3,4,userId)
    key3=QTOPIC_TOTAL_COUNT%(3)
    key4=QTOPIC_USER_PLAN%(3,4)
    key5=QTOPIC_TODAY_GOAL_DONE_TOPIC%(3,4,toDay,userId)
    key6=QTOPIC_TODAY_DONE_TOPIC%(3,4,toDay)
    key7=QTOPIC_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    key8=QTOPIC_CORRECT_COUNT%(3,4,userId)
    key9=QTOPIC_WRONG_COUNT%(3,4,userId)
    key10=QTOPIC_MEMORY_CARD%(3,4,userId)
    key11=QTOPIC_CORRECT_COUNT%(3,6,userId)
    key12=QTOPIC_WRONG_COUNT%(3,6,userId)
    key13=QTOPIC_MEMORY_CARD%(3,6,userId)
    key14=QTOPIC_MEMORY_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    redisConnect.hdel(key4,userId)
    redisConnect.hdel(key6, userId)
    redisConnect.delete(key1,key2,key5,key7,key8,key9,key10,key14)
    redisConnect.sadd(key1,*(1615,1616,1617,1697,1698))
    redisConnect.sadd(key2,1617)
    backValue=redisConnect.hget(key3,4)
    redisConnect.hset(key3,4,1200)
    redisConnect.hset(key8,1624,3)
    redisConnect.hset(key11,1394,3)
    redisConnect.hset(key9,1624,5)
    redisConnect.hset(key12,1394,5)
    redisConnect.sadd(key10,1624)
    redisConnect.sadd(key13,1394)
    yield
    redisConnect.hset(key3, 4, backValue)
    sql="update tms_qtopic set subject_type=4 where subject_type=9 and (id<1589 or id >1698);"
    curDic.execute(sql)
    sql="update tms_qtopic set subject_type=6 where subject_type=10 and (id<1376 or id >1409);"
    curDic.execute(sql)
    sql="update tms_exam_point set frequency=3 where id=1662;"
    curDic.execute(sql)
    con.commit()



@pytest.fixture(scope="function")
def setQtopicPlan(redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    toDay=arrow.now().format().split(" ")[0]
    key=QTOPIC_USER_PLAN%(3,4)
    key1=QTOPIC_TODAY_GOAL_DONE_TOPIC%(3,4,toDay,userId)
    key2=QTOPIC_TODAY_DONE_TOPIC%(3,4,toDay)
    redisConnect.hset(key,userId,10)
    redisConnect.sadd(key1,*(1615,1616,1617,1697,1698))
    redisConnect.hset(key2,userId,4)

@pytest.fixture(scope="function")
def setDoneTopic (redisConnect,mysqlConnect_sydr,login):
    key = QTOPIC_TOTAL_COUNT % (3)
    redisConnect.hset(key,4,55)
    yield
    redisConnect.hset(key, 4, 1200)


@pytest.fixture(scope="function")
def setDoneTopic1 (redisConnect,mysqlConnect_sydr,login):
    key = QTOPIC_TOTAL_COUNT % (3)
    redisConnect.hset(key,4,3)
    yield
    redisConnect.hset(key, 4, 1200)

@pytest.fixture(scope="function")
def setQtopicNotShow (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update tms_qtopic_config set shorthand_status=0,quick_questions_stauts=0 where id=4;"
    curDic.execute(sql)
    con.commit()
    yield
    sql="update tms_qtopic_config set shorthand_status=1,quick_questions_stauts=1 where id=4;"
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="function")
def setQtopicStatus (redisConnect,mysqlConnect_sydr):
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql = "update tms_qtopic set publish_status=3 where id=1619;"
    curDic.execute(sql)
    con.commit()
    yield
    sql = "update tms_qtopic set publish_status=2 where id=1619;"
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="function")
def setAllDone(redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key1=QTOPIC_DONE_TOPICS%(3,4,userId)
    topics=[i for i in range(1589,1699)]
    redisConnect.sadd(key1,*topics)
    yield
    redisConnect.sadd(key1,*(1615,1616,1617,1697,1698))


@pytest.fixture(scope="function")
def setRoundTopic (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key=QTOPIC_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    topicIds=[1394,1402,1378,1379,1408,1,2,3,4]
    redisConnect.sadd(key,*topicIds)


@pytest.fixture(scope="function")
def setRoundTopic1 (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key=QTOPIC_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    topicIds=[1394,1402,1378,1379,1408,1399,1382,1395,1388,1392,1376,1,2,3,4]
    redisConnect.sadd(key,*topicIds)

@pytest.fixture(scope="function")
def setRoundTopic2 (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key=QTOPIC_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    topicIds = [1394, 1398, 1402, 1406, 1378, 1381, 1384, 1399, 1382, 1395,
                1379, 1388, 1408, 1392, 1376, 1403, 1385,1386, 1389, 1393,
                1397,1400,1405,1409,1377,1380,1383]
    redisConnect.sadd(key,*topicIds)


@pytest.fixture(scope="function")
def clearForTopic (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    toDay=arrow.now().format().split(" ")[0]
    key3=QTOPIC_USER_PLAN%(3,6)
    key4=QTOPIC_TODAY_GOAL_DONE_TOPIC%(3,6,toDay,userId)
    key7=QTOPIC_CORRECT_POOL%(3,6,userId)
    key8=QTOPIC_WRONG_POOL%(3,6,userId)
    key10=QTOPIC_MEMORY_CARD%(3,6,userId)
    key11=QTOPIC_DONE_TOPICS%(3,6,userId)
    key12=QTOPIC_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)

    redisConnect.hdel(key3,userId)
    redisConnect.srem(key4,1398)
    redisConnect.srem(key7,1398)
    redisConnect.srem(key8,1398)
    redisConnect.srem(key10,1398)
    redisConnect.srem(key11,1398)
    redisConnect.srem(key12,1398)


@pytest.fixture(scope="function")
def setAutoJoinCard (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    con:Connection=mysqlConnect_sydr[1]
    curDic: Cursor = con.cursor(cursor=DictCursor)
    sql="update ums_user_exam_info set memory_card=1 where user_id={}".format(userId)
    curDic.execute(sql)
    con.commit()

@pytest.fixture(scope="class")
def initMemoryCard(redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key1= QTOPIC_MEMORY_CARD % (3, 4, userId)
    key2= QTOPIC_MEMORY_CARD % (3, 6, userId)
    redisConnect.delete(key1,key2)
    topicList=[i for i in range(1589,1699)]
    redisConnect.sadd(key1,*topicList)
    topicList=[i for i in range(1376,1410)]
    redisConnect.sadd(key2,*topicList)
    key3=QTOPIC_CORRECT_COUNT%(3,6,userId)
    key4=QTOPIC_STATISTICS_CORRECT%(3,6)
    key5=QTOPIC_CORRECT_POOL%(3,6,userId)
    key6=QTOPIC_WRONG_POOL%(3,6,userId)
    key7=QTOPIC_WRONG_COUNT%(3,6,userId)
    key8=QTOPIC_STATISTICS_WRONG%(3,6)
    key9=QTOPIC_MEMORY_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    redisConnect.hdel(key3,1403)
    redisConnect.hdel(key4,1403)
    redisConnect.srem(key5,1403)
    redisConnect.srem(key6,1403)
    redisConnect.hdel(key7,1403)
    redisConnect.hdel(key8,1403)
    redisConnect.srem(key9,1403)
    toDay=arrow.now().format().split(" ")[0]
    key10=QTOPIC_USER_PLAN%(3,6)
    redisConnect.hset(key10,userId,10)
    key11=QTOPIC_TODAY_GOAL_DONE_TOPIC%(3,6,toDay,userId)
    key12=QTOPIC_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    redisConnect.delete(key11,key12)


@pytest.fixture(scope="function")
def removeTopicId (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key= QTOPIC_MEMORY_CARD % (3, 6, userId)
    redisConnect.srem(key,1393)
    yield
    redisConnect.sadd(key,1393)




@pytest.fixture(scope="function")
def setRoundTopicCard (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key=QTOPIC_MEMORY_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    topicIds=[1394,1402,1378,1379,1408,1,2,3,4]
    redisConnect.sadd(key,*topicIds)



@pytest.fixture(scope="function")
def setRoundTopicCard1 (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key=QTOPIC_MEMORY_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    topicIds=[1394,1402,1378,1379,1408,1399,1382,1395,1388,1392,1376,1,2,3,4]
    redisConnect.sadd(key,*topicIds)



@pytest.fixture(scope="function")
def setRoundTopicCard2 (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key=QTOPIC_MEMORY_CURRENT_ROUND_DONE_TOPICS%(3,6,userId)
    topicIds = [1394, 1398, 1402, 1406, 1378, 1381, 1384, 1399, 1382, 1395,
                1379, 1388, 1408, 1392, 1376, 1403, 1385,1386, 1389, 1393,
                1397,1400,1405,1409,1377,1380,1383]
    redisConnect.sadd(key,*topicIds)


@pytest.fixture(scope="function")
def removeTopicCard (redisConnect,mysqlConnect_sydr,login):
    userId=login.headers.get("userId")
    key= QTOPIC_MEMORY_CARD % (3, 6, userId)
    topics=[i for i in range(1376,1386)]
    redisConnect.srem(key,*topics)






